﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;
using WebApplication1.domain;

namespace WebApplication1.dormitoryAdministrator
{
    public partial class StudentStayManagement2 : System.Web.UI.Page
    {
        List<domain.Student> students = new List<domain.Student>();
        List<String> list = new List<String>();
        String hostel;
        protected void Page_Load(object sender, EventArgs e)
        {
            hostel = (String)Application["hostel"];
            Panel1.Visible = false;
            if (!Page.IsPostBack)
            {
                this.setDownList();
                this.getList();
            }
        }

        public DataTable getHostel() {
           
            String sql = "SELECT * from hostel ";
            DataTable hostelName = MySqlHelper.GetDataTable(sql);
            return hostelName;
        }

        public void setDownList() {
            addHostel.Items.Clear();
            DataTable dataTable = getHostel(); 
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                addHostel.Items.Add(new ListItem(
                dataTable.Rows[i][1].ToString()));
                addHostel.Items[i].Value = dataTable.Rows[i][0].ToString();
            }
            addHostel.Items.Insert(0, new ListItem("请选择宿舍楼", "绑定数据"));
        }

        private void bind(DataTable Adt)
        {
            int row = Adt.Rows.Count;

            for (int r = 0; r < row; r++)
            {
                DormitoryInfo dormitoryInfo = new DormitoryInfo();
                domain.Student student = new domain.Student();
                student.Id = (int)Adt.Rows[r][0];
                dormitoryInfo.HostelName = Adt.Rows[r][1].ToString();
                dormitoryInfo.Name = Adt.Rows[r][2].ToString();
                student.Name = Adt.Rows[r][3].ToString();
                student.No = Adt.Rows[r][4].ToString();
                student.ClassName = Adt.Rows[r][5].ToString();
                student.DormitoryInfo = dormitoryInfo;
                students.Add(student);
            } 
            GridView1.DataSource = students;
            GridView1.DataBind();

        }
        private void getList()
        {
            String sql = "SELECT s.id,h.`name`,d.dormitory_number,s.`name` name,s.`no`,s.class_name FROM `student` s LEFT JOIN dormitory d on d.id = s.dormitory_id LEFT JOIN hostel h on h.id = d.hostel_id where h.`name` = '" + hostel + "'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        protected void s_Click(object sender, EventArgs e)
        {
            string name = selectName.Text;
            String sql = "SELECT s.id,h.`name`,d.dormitory_number,s.`name` name,s.`no`,s.class_name FROM `student` s LEFT JOIN dormitory d on d.id = s.dormitory_id LEFT JOIN hostel h on h.id = d.hostel_id where d.dormitory_number like '%" + name + "%' and h.`name` = '" + hostel + "'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // DataRow row = MySqlHelper.GetDataRow(sql);
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            string id = ((Label)row.FindControl("id")).Text;
            String sql = "delete from student where id = " + id;
            int result = MySqlHelper.delete(sql);
            if (result == 1)
            {
                Response.Write("<script>alert('删除成功！')</script>");
                getList();
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
        }
        //让当前行处于修改状态
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
            GridView1.EditIndex = e.NewEditIndex;    
            getList();
        }
        //取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            getList();
        }
        //更新至数据库
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            GridViewRow row = GridView1.Rows[index];
            string id = ((Label)row.FindControl("id")).Text;
            string dNo = ((TextBox)row.FindControl("dNo")).Text;
            string drpHostel = ((DropDownList)row.FindControl("drpHostel")).SelectedValue;
            String sql = "select * from dormitory  where dormitory_number  = '" + dNo + "' and hostel_id = " + drpHostel;
            DataTable data = MySqlHelper.GetDataTable(sql);
            if (data.Rows.Count == 0){
                Response.Write("<script>alert('更新失败，宿舍号不存在！');</script>");
            }
            else {
                int did = (int)data.Rows[0][0];
                String sql2 = "update student set dormitory_id = '" + did + "' where id = " + id;
                MySqlHelper.GetDataTable(sql2);
                Response.Write("<script>alert('更新成功');</script>");
                GridView1.EditIndex = -1;
                getList();
            }
            //2.获取第一行数据
           
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Response.Write("当前行的下标：" + e.Row.RowIndex+"<br/>");
            //Response.Write("当前行的类型：" + e.Row.RowType.ToString() + "<br/>");
            //Response.Write("当前行的状态：" + e.Row.RowState.ToString() + "<br/>");
            string hostel = "";
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label sNo = e.Row.FindControl("studentNo") as Label;

                if (sNo != null)
                {
                    String sql = "SELECT s.id,h.`name`,d.dormitory_number,s.`name` name,s.`no`,s.class_name FROM `student` s LEFT JOIN dormitory d on d.id = s.dormitory_id LEFT JOIN hostel h on h.id = d.hostel_id where s.no = '" + sNo.Text + "'";
                    DataTable data = MySqlHelper.GetDataTable(sql);
                    //2.获取第一行数据
                    hostel = data.Rows[0][1].ToString();
                   
                }
            }
            //普通项处于编辑状态,交替项的编辑状态
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))
            {


                DropDownList drpHostel = e.Row.FindControl("drpHostel") as DropDownList;
                drpHostel.Items.Clear();
                DataTable dataTable = getHostel();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    drpHostel.Items.Add(new ListItem(
                    dataTable.Rows[i][1].ToString()));
                    drpHostel.Items[i].Value = dataTable.Rows[i][0].ToString();
                }
                
                if (hostel == "") {
                    drpHostel.Items.Insert(0, new ListItem("请选择宿舍楼", "绑定数据"));
                }
                if (drpHostel != null)
                    drpHostel.SelectedValue = hostel;//设置性别下拉列表的选中状态是当前学员的性别
            }
        }

        protected void add_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void okAdd_Click(object sender, EventArgs e)
        {
            
            string no = addNo.Text;
            string name = addName.Text;
            string clas = addClass.Text;
            string dormitory =  addDormitory.Text;
            string hostel = addHostel.SelectedValue;
            String sql = "select * from dormitory  where dormitory_number  = '" + dormitory + "' and hostel_id = '" + hostel;
            DataTable data = MySqlHelper.GetDataTable(sql);
            if (data.Rows.Count == 0)
            {
                Response.Write("<script>alert('添加失败，此宿舍楼不存在"+ dormitory+"寝室！');</script>");
            }
            else {
                int did = (int)data.Rows[0][0];
                String sql2 = "insert into student (no,name,class_name,dormitory_id) values ('" + no + "','" + name + "','" + clas + "','" + did+"')";
                MySqlHelper.GetDataTable(sql2);
                Response.Write("<script>alert('添加成功');</script>");
                getList();
                Panel1.Visible = false;
            }
           
        }

        protected void escAdd_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void selectAll_Click(object sender, EventArgs e)
        {
            getList();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.getList();
        }
    }
}